package com.aftersale.platform.service;

import com.aftersale.platform.entity.Ticket;
import com.aftersale.platform.mapper.TicketMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class TicketService {

    @Autowired
    private TicketMapper ticketMapper;

    public List<Ticket> findAll() {
        return ticketMapper.findAll();
    }

    public Ticket findById(Integer id) {
        return ticketMapper.findById(id);
    }

    public List<Ticket> findByUserId(Integer userId) {
        return ticketMapper.findByUserId(userId);
    }

    public void save(Ticket ticket) {
        if (ticket.getSubmitDate() == null) {
            ticket.setSubmitDate(LocalDate.now()); // 默认提交日期
        }
        if (ticket.getStatus() == null) {
            ticket.setStatus(Ticket.TicketStatus.PENDING); // 默认待处理
        }
        if (ticket.getProgress() == null || ticket.getProgress().isEmpty()) {
            ticket.setProgressList(List.of("已提交")); // 默认进度
        }
        ticketMapper.save(ticket);
    }

    public void update(Ticket ticket) {
        ticketMapper.update(ticket);
    }

    public void delete(Integer id) {
        ticketMapper.delete(id);
    }
}